package com.linshidream.intrachat.simplenio.pipeline;

import com.linshidream.intrachat.simplenio.pipeline.context.MiniHandlerContext;
import com.linshidream.intrachat.simplenio.pipeline.handler.MiniChannelInboundHandler;
import com.linshidream.intrachat.simplenio.pipeline.handler.MiniChannelOutboundHandler;

/**
 * Created on 2025/8/19 18:21
 *
 * @author linshidream
 * @version 1.0.0
 * @description 日志处理器
 */

public class LoggerHandler implements MiniChannelInboundHandler , MiniChannelOutboundHandler {
    @Override
    public void channelActive(MiniHandlerContext ctx) {
        System.out.println(ctx.handlerName() + ",通道已激活");
        // 继续往下传
        ctx.fireChannelActive();
    }

    @Override
    public void channelRead(MiniHandlerContext ctx, Object msg) {
        System.out.println(ctx.handlerName() + ", 收到消息: " + msg);
        // 继续往下传
        ctx.fireChannelRead(msg);
    }

    @Override
    public void channelInactive(MiniHandlerContext ctx) {
        System.out.println(ctx.handlerName() + ",通道已关闭");
        // 继续往下传
        ctx.fireChannelInactive();
    }

    @Override
    public void exceptionCaught(MiniHandlerContext ctx, Throwable cause) {
        System.err.println(ctx.handlerName() + ", 发生异常: " + cause.getMessage());
        // 继续往下传
        ctx.fireExceptionCaught(cause);
    }

    @Override
    public void channelWrite(MiniHandlerContext ctx, Object msg) {
        System.out.println(ctx.handlerName() + ", 写消息到通道: " + msg);
        // 继续往下传
        ctx.fireWrite(msg);
    }
}
